package com.example.bookstore.security

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
import org.springframework.security.core.AuthenticationException
import org.springframework.security.web.AuthenticationEntryPoint
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 * Created by IntelliJ IDEA.
 * User: lvshe
 * Date: 2019/8/13
 * Time: 21:17
 */
class AuthenticationToJsonEntryPoint: AuthenticationEntryPoint {

    override fun commence(request: HttpServletRequest, response: HttpServletResponse, authException: AuthenticationException) {
        response.status = HttpServletResponse.SC_FORBIDDEN
        response.contentType = "application/json; charset=UTF-8"
        val responseJson = hashMapOf("message" to "未登录，需要验证信息")
        response.writer.write(MappingJackson2HttpMessageConverter().objectMapper.writeValueAsString(responseJson))

        response.writer.flush()
        response.writer.close()
    }
}